﻿using System;
using System.Data.Odbc;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using Cat;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Thesis_2
{
    public partial class frmUserReg : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        public frmUserReg()
        {
            InitializeComponent();
            btnRegister.Click += new EventHandler(btnRegister_Click);
            btnGetDetails.Click += new EventHandler(btnGetDetails_Click);
            txtEmail_Address.Validating += new CancelEventHandler(txtEmail_Address_Validating);
            txtUsername.Validating += new CancelEventHandler(txtUsername_Validating);
            txtName.KeyPress += new KeyPressEventHandler(txtName_KeyPress);
            txtContact_no.KeyPress += new KeyPressEventHandler(txtContact_no_KeyPress);
            btnCheckAvailability.Click += new EventHandler(btnCheckAvailability_Click);
            btnNewRecord.Click += new EventHandler(btnNewRecord_Click);
            
        }

        void btnNewRecord_Click(object sender, EventArgs e)
        {
            reset();
        }
        void btnCheckAvailability_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtUsername.Text == "")
                {
                    MessageBox.Show("Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.Focus();
                    return;
                }
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "select Username from tbluserinfo where Username='" + txtUsername.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Username not available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (!rdr.Read())
                {
                    MessageBox.Show("Username available", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtUsername.Focus();

                }
                if ((rdr != null))
                {
                    rdr.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        void txtContact_no_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        void txtName_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space);
        }
        void txtUsername_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex("^[a-zA-Z0-9_]");
            if (txtUsername.Text.Length > 0)
            {
                if (!rEMail.IsMatch(txtUsername.Text))
                {
                    MessageBox.Show("Only letters,numbers and underscore is allowed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.SelectAll();
                    e.Cancel = true;
                }
            }
        }
        void txtEmail_Address_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]{2,28}[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (txtEmail_Address.Text.Length > 0)
            {
                if (!rEMail.IsMatch(txtEmail_Address.Text))
                {
                    MessageBox.Show("Invalid email address", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtEmail_Address.SelectAll();
                    e.Cancel = true;
                }
            }
        }
        void btnGetDetails_Click(object sender, EventArgs e)
        {
            frmListofuser frmL = new frmListofuser();
            frmL.Show();
            this.Hide();
        }
        void reset()
        {
            txtUsername.Text = "";
            cmbUserType.SelectedIndex = -1;
            txtPassword.Text = "";
            txtContact_no.Text = "";
            txtName.Text = "";
            txtEmail_Address.Text = "";
            btnRegister.Enabled = true;
            txtUsername.Focus();
        }
        void btnRegister_Click(object sender, EventArgs e)
        {
            #region error trap
            if (txtUsername.Text == "")
            {
                MessageBox.Show("Please enter username", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUsername.Focus();
                return;
            }
            if (cmbUserType.Text == "")
            {
                MessageBox.Show("Please select user type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                cmbUserType.Focus();
                return;
            }
            if (txtPassword.Text == "")
            {
                MessageBox.Show("Please enter password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtPassword.Focus();
                return;
            }
            if (txtName.Text == "")
            {
                MessageBox.Show("Please enter name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtName.Focus();
                return;
            }
            if (txtContact_no.Text == "")
            {
                MessageBox.Show("Please enter contact no.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtContact_no.Focus();
                return;
            }
            if (txtEmail_Address.Text == "")
            {
                MessageBox.Show("Please enter email", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtEmail_Address.Focus();
                return;
            }
            #endregion
            try
            {
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct = "Select Username from tbluserinfo where Username='" + txtUsername.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Username Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtUsername.Text = "";
                    txtUsername.Focus();

                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;
                }
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string ct1 = "Select Email from tbluserinfo where Email='" + txtEmail_Address.Text + "'";
                cmd = new OdbcCommand(ct1);
                cmd.Connection = con;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Email ID Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtEmail_Address.Text = "";
                    txtEmail_Address.Focus();


                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;

                }
                con = new OdbcConnection(cs.DBConn);
                con.Open();
                string cb = "Insert into tbluserinfo(Username,Password,Name,ContactNo,Email,Usertype) VALUES ('" + txtUsername.Text + "','" + txtPassword.Text + "','" + txtName.Text + "','" + txtContact_no.Text + "','" + txtEmail_Address.Text + "','" + cmbUserType.Text + "')";

                cmd = new OdbcCommand(cb);
                cmd.Connection = con;
                cmd.ExecuteReader();
                con.Close();
                MessageBox.Show("Successfully Registered", "User", MessageBoxButtons.OK, MessageBoxIcon.Information);

                btnRegister.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
